/* Analysis of Japan data */ 
/* data assembled by Amy Catalinac and Shiro Kuriwaki */
/* Alastair Smith May 10th 2018 */


clear
capture cd "D:/Users/Lucia/Dropbox/NYU/Papers/Catalinac & Motolinia/Help_Dec2019/helpDec11/"
capture cd "D:/Users/Lucia/Dropbox/NYU/Papers/Catalinac & Motolinia/Help_Dec2019/helpDec11/"
set more off


import delimited "out_final_utf8.csv", encoding(utf-8) 
destring _all, replace ignore(NA)

save temp,replace 
*/

use temp,clear 

/*Sturcture of the data: hor_electoral_district  and unique_municipality_code and year */

/* Unit of analysis: transfers per capita in the literature
But why not implement as total trasfers and have independent vars as number of people */

/* following horiuchi and Saito 2003 we need to include a maasure of repreentation*/ 
/* mun_stxlo -- local tax allocation -- formula based  - least likely affected by votes*/
/* mun_stxls -Local Allocation Tax (special) -- more discretion -  million Yen*/
/* mun_ngaid -- National treasury disbursements*/
/* mun_ipbf -- local tax raised */
/* mun_dpbf -- Standardized fiscal demand  -old people, number police etc. */
/* mun_population -- total /* variable for most votes */ population */
/* mun_DIDpopulation -- urban pop */ 
gen mun_needy = mun_pop_14_and_under+mun_pop_65_and_over /* old and young */
/* mun employment primary -- pop in primary agriculture */
/* mun is seirei -- big urban area */ 
/*mun disaster stricken --  hit by earthquake */

/****** Problem : num_mun should be the number of municipalities in a hor but too large in some cases  *****/ 

sort hor_electoral_district year
by hor_electoral_district year: gen num_mun=_N


gen LDPgov=1
replace LDPgov=0 if year==1993 |year==1994 |year==2009|year==2010|year==2011 
gen reform =(year>1994) 
/* what is difference between hor_district and hor_electoral_district */
sort code year 


#delimit;
li code muncode_num year hor_electoral_district mun_ngaid cand_01_votes if 
(code==code[_n-1] &year==year[_n-1])|
(code==code[_n+1] &year==year[_n+1]);

#delimit;
count if 
(code==code[_n-1] &year==year[_n-1])|
(code==code[_n+1] &year==year[_n+1]);

#delimit;
gen waitaminute=1 if 
(code==code[_n-1] &year==year[_n-1])|
(code==code[_n+1] &year==year[_n+1]);

/* there are  988  duplicate observations: Lets finds out where they are from */ 
#delimit;
gen nocode=(code==.) ;
tab nocode waitaminute; /* 99  cases have no code -- cannot match sudsidy and election data*/ 
tab waitaminute split if nocode==0; /* 749 cases correspnd to the municipalities that span two electoral districts*/ 
/* There are 132 cases left to explain */ 
li code muncode_num year if waita==1 &split==0 &nocode==0; 
drop if code==. ; 
drop if split==1;
set seed 339487731; 
gen u=runiform();
sort code year u; /* there might be differences in duplicate order and so make sure that same observation is dropped for replication purposes*/ 
drop if (code==code[_n-1] &year==year[_n-1]);
drop u;
sort code year;
tsset code year;

/* Do analysis at the per capita level -- common in the literature */ 
gen ngaid_pc= mun_ngaid/mun_population;
gen stxlo_pc= mun_stxlo/mun_population;
gen stxls_pc= mun_stxls/mun_population;
gen needy_pc=mun_needy/mun_population;
gen urban_pc= mun_didpopulation/mun_population ;
gen lnpop=log(mun_population);
gen lndensity=log(mun_population_density);
gen primary_pc=mun_employment_primary/mun_population ;
gen fis_demand=mun_dpbf/mun_population;
gen fis_rev=mun_ipbf/mun_population;


/* Examine the variance in size of municipality */ 
sort hor year ;
by hor year: egen meanlnpop=mean(lnpop);
by hor year: egen sdlnpop=sd(lnpop);
by hor year: egen maxlnpop=max(lnpop);


foreach i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 {;
	gen win_`i'=( `i'<= totseat_in_electoral_district) /* generate a variable to indicate if the candidate wins a seat*/ ;
	if `i'<10 {;
		replace cand_0`i'_votes=0 if cand_0`i'_votes==.;
		gen senior_`i'=0;
		gen LDP_`i'=(cand_0`i'_pty=="LDP" );
		 /* LDP party "自民党"*/;
		gen LDP_`i'plus=(cand_0`i'_pty=="LDP" |cand_0`i'_pty=="LDPI" ) ;
		/* LDP party "自民党" plus Independents adopted by LDP*/
		gen COM_`i'=(cand_0`i'_pty=="JCP" |cand_0`i'_pty=="JSP" );
		*rename cand_0`i'_votes_electoral_district cand_0`i'_votes_electoral_district
		replace cand_0`i'_votes=0 if cand_0`i'_votes==. &hor_electoral_district~=. /* replace missing with zero for later summations */;
		replace  cand_0`i'_votes_district=0 if cand_0`i'_votes_district==.  &hor_electoral_district~=.;
		gen Vshare`i' = cand_0`i'_votes/mun_voted;
		gen VshareVP`i' = cand_0`i'_votes/mun_voting_pop;
		gen PropSupport`i' = cand_0`i'_votes/cand_0`i'_votes_district;
		};
		else if `i'>9 {;
		replace cand_`i'_votes=0 if cand_`i'_votes==.;
		gen senior_`i'=0;
		gen LDP_`i'=(cand_`i'_pty=="LDP" );
		 /* LDP party "自民党"*/
		gen LDP_`i'plus=(cand_`i'_pty=="LDP" |cand_`i'_pty=="LDPI" ) ;
		/* LDP party "自民党" plus Independents adopted by LDP*/
		gen COM_`i'=(cand_`i'_pty=="JCP" |cand_`i'_pty=="JSP" );
		*rename cand_`i'_votes_electoral_distrct cand_`i'_votes_electoral_district
		replace cand_`i'_votes=0 if cand_`i'_votes==.  &hor_electoral_district~=.;
		replace cand_`i'_votes_district=0 if cand_`i'_votes_district==.  &hor_electoral_district~=.;
		gen Vshare`i' = cand_`i'_votes/mun_voted;
		gen VshareVP`i' = cand_`i'_votes/mun_voting_pop;
		gen PropSupport`i' = cand_`i'_votes/cand_`i'_votes_district;
		};
	};
	
foreach i in 1 2 3 4 5 6 {;
	replace senior_`i'=1 if (cand_0`i'_terms>5 & win_`i'==1 & cand_0`i'_terms~=.)|(cand_0`i'_terms>=5 & win_`i'==0& cand_0`i'_terms~=.);
	};
	label var senior_1 "Candidate has won at least 5 previous terms in office"	;
label var Vshare1 "By Mun: vote share for candidate 1 -- Vote share wo turnout factor"	;
label var VshareVP1 "By Mun: votes for 1 divided by number of voters in mun -- turnout matters";
label var PropSupport1 "Proportion of total votes for cand1 from mun";
capture drop num_mun;
sort hor_electoral_district year;
by hor_electoral_district year: gen num_mun=_N;
by hor_electoral_district year: egen topSupport1=max(PropSupport1)	;
by hor_electoral_district year: gen IsTopSupport1=(topSupport1==PropSupport1)	;
label var IsTopSupport1 "0/1: mun that provided candidate 1 with most votes";


/* Best supporter */ 
#delimit;
gen best_VshareVP=max(win_1*VshareVP1,win_2*VshareVP2,win_3*VshareVP3,
win_4*VshareVP4,win_5*VshareVP5,win_6*VshareVP6);
label var best_VshareVP "highest Vote ShareVP given to any winning LDP candidate";
gen sum_VshareVP=win_1*VshareVP1+win_2*VshareVP2+win_3*VshareVP3+
win_4*VshareVP4+win_5*VshareVP5+win_6*VshareVP6;
label var sum_VshareVP "Sum of Vote ShareVP given to winning LDP candidates";
/* support for LDP */
gen bestLDP_VshareVP=max(LDP_1*win_1*VshareVP1,LDP_2*win_2*VshareVP2,LDP_3*win_3*VshareVP3,
LDP_4*win_4*VshareVP4,LDP_5*win_5*VshareVP5,LDP_6*win_6*VshareVP6);
label var bestLDP_VshareVP "highest Vote ShareVP given to any winning LDP candidate";
gen sumLDP_VshareVP=LDP_1*win_1*VshareVP1+LDP_2*win_2*VshareVP2+LDP_3*win_3*VshareVP3+
LDP_4*win_4*VshareVP4+LDP_5*win_5*VshareVP5+LDP_6*win_6*VshareVP6;
label var sumLDP_VshareVP "Sum of Vote ShareVP given to winning LDP candidates";
/* support LDP plus */
gen bestLDPp_VshareVP=max(LDP_1p*win_1*VshareVP1,LDP_2p*win_2*VshareVP2,LDP_3p*win_3*VshareVP3,
LDP_4p*win_4*VshareVP4,LDP_5p*win_5*VshareVP5,LDP_6p*win_6*VshareVP6);
label var bestLDP_VshareVP "highest Vote ShareVP given to any winning LDPplus candidate";
gen sumLDPp_VshareVP=LDP_1p*win_1*VshareVP1+LDP_2p*win_2*VshareVP2+LDP_3p*win_3*VshareVP3+
LDP_4p*win_4*VshareVP4+LDP_5p*win_5*VshareVP5+LDP_6p*win_6*VshareVP6;
label var sumLDPp_VshareVP "Sum of Vote ShareVP given to winning LDPplus candidates";

/* Support for communists or socialists */ 
gen bestCOM_VshareVP=max(COM_1*win_1*VshareVP1,COM_2*win_2*VshareVP2,COM_3*win_3*VshareVP3,
COM_4*win_4*VshareVP4,COM_5*win_5*VshareVP5,COM_6*win_6*VshareVP6);
label var bestCOM_VshareVP "highest Vote ShareVP given to any winning communist candidate";
gen sumCOM_VshareVP=COM_1*win_1*VshareVP1+COM_2*win_2*VshareVP2+COM_3*win_3*VshareVP3+
COM_4*win_4*VshareVP4+COM_5*win_5*VshareVP5+COM_6*win_6*VshareVP6;
label var sumCOM_VshareVP "Sum of Vote ShareVP given to winning LDP candidates";


 
/* Placebo tests - non LDP winner,  losing LDP candidates.  */

#delimit; 
gen bestSeniorLDP_VshareVP=max(senior_1*LDP_1*win_1*VshareVP1,senior_2*LDP_2*win_2*VshareVP2,
senior_3*LDP_3*win_3*VshareVP3,
senior_4*LDP_4*win_4*VshareVP4,senior_5*LDP_5*win_5*VshareVP5,senior_6*LDP_6*win_6*VshareVP6);

#delimit; 
gen sumSeniorLDP_VshareVP =0 ;
#delimit; 
capture drop temp;
foreach t in 1 2 3 4 5 6  {;
	gen temp= senior_`t'*LDP_`t'*win_`t'*VshareVP`t';
	replace sumSeniorLDP_VshareVP=sumSeniorLDP_VshareVP+temp if temp~=.;
	drop temp;
	};
	*/
#delimit; 

/* define the best Vshare but for non LDP parties */
gen bestnonLDP_VshareVP=max((1-LDP_1)*win_1*VshareVP1,(1-LDP_2)*win_2*VshareVP2,(1-LDP_3)*win_3*VshareVP3,
(1-LDP_4)*win_4*VshareVP4,(1-LDP_5)*win_5*VshareVP5,(1-LDP_6)*win_6*VshareVP6);

gen sumnonLDP_VshareVP =0 ;
#delimit; 
capture drop temp;
foreach t in 1 2 3 4 5 6  {;
	gen temp= (1-LDP_`t')*win_`t'*VshareVP`t';
	replace sumnonLDP_VshareVP=sumnonLDP_VshareVP+temp if temp~=.;
	drop temp;
	};

/* LDP losing candidates */ #delimit;
gen bestloseLDP_VshareVP=max(LDP_1*(1-win_1)*VshareVP1,LDP_2*(1-win_2)*VshareVP2,LDP_3*(1-win_3)*VshareVP3,
LDP_4*(1-win_4)*VshareVP4,LDP_5*(1-win_5)*VshareVP5,LDP_6*(1-win_6)*VshareVP6,LDP_7*(1-win_7)*VshareVP7,LDP_8*(1-win_8)*VshareVP8,
LDP_9*(1-win_9)*VshareVP9,LDP_10*(1-win_10)*VshareVP11,LDP_12*(1-win_12)*VshareVP12,LDP_13*(1-win_13)*VshareVP13,
LDP_1*(1-win_14)*VshareVP14,LDP_15*(1-win_15)*VshareVP15,LDP_16*(1-win_16)*VshareVP16,LDP_17*(1-win_17)*VshareVP17);

gen sumloseLDP_VshareVP =0 ;
#delimit; 
capture drop temp;
foreach t in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 {;
	gen temp= LDP_`t'*(1-win_`t')*VshareVP`t';
	replace sumloseLDP_VshareVP=sumloseLDP_VshareVP+temp if temp~=.;
	drop temp;
	};
gen bestnonLDPplus_VshareVP=max((1-LDP_1plus)*win_1*VshareVP1,(1-LDP_2plus)*win_2*VshareVP2,(1-LDP_3plus)*win_3*VshareVP3,
(1-LDP_4plus)*win_4*VshareVP4,(1-LDP_5plus)*win_5*VshareVP5,(1-LDP_6plus)*win_6*VshareVP6);

gen sumnonLDPplus_VshareVP =0 ;
#delimit; 
capture drop temp;
foreach t in 1 2 3 4 5 6  {;
	gen temp= (1-LDP_`t'plus)*win_`t'*VshareVP`t';
	replace sumnonLDPplus_VshareVP=sumnonLDPplus_VshareVP+temp if temp~=.;
	drop temp;
	};	
	
sum bestLDP_VshareVP bestLDPp_VshareVP  bestnonLDP_VshareVP  
bestloseLDP_VshareVP  ;
sum sumLDP_VshareVP sumLDPp_VshareVP  sumnonLDP_VshareVP  
sumloseLDP_VshareVP  ;
#delimit;

gen LDPseats=(LDP_1*win_1+LDP_2*win_2+LDP_3*win_3+LDP_4*win_4+LDP_5*win_5+LDP_6*win_6)/totseat_in_electoral_district;

gen safe = (cand_01_votes_district -cand_02_votes_district)/voted_in_electoral_district	;
label var safe "At hor level: difference in votes between 1st and 2nd candidate"	;

	#delimit;
/* Capture information on which candidates won at the previous election (call incumbents )*/
/* create a list of incumbents */ 
sort code year;
tsset code year;
foreach i in 1 2 3 4 5 6 {;
	gen i`i'="";
	replace i`i'=cand_0`i'_name if `i'<= totseat_in_electoral_district;
	by code: gen Li`i'=i`i'[_n-1] if i`i'[_n-1]~="NA";
	by code: replace Li`i'=Li`i'[_n-1] if Li`i'[_n-1]~="" & Li`i'=="";
	drop i`i';
	};
/* the variable Li1 li2 etc list the incumbent from the prior election (assuming no deaths) */ 
foreach i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 {;
	
	if `i'<10 {;
		gen IsIncumbent`i' = 0  if hor~=.;
		replace IsIncumbent`i' = 1 if  cand_0`i'_name ==Li1 ;
		replace IsIncumbent`i' = 1 if  cand_0`i'_name ==Li2 ;
		replace IsIncumbent`i' = 1 if  cand_0`i'_name ==Li3 ;
		replace IsIncumbent`i' = 1 if  cand_0`i'_name ==Li4 ;
		replace IsIncumbent`i' = 1 if  cand_0`i'_name ==Li5 ;
		replace IsIncumbent`i' = 1 if  cand_0`i'_name ==Li6 ;
	
		};
		else if `i'>9 {;
		gen IsIncumbent`i' = 0 if hor~=.;
		replace IsIncumbent`i' = 1 if  cand_`i'_name ==Li1 ;
		replace IsIncumbent`i' = 1 if  cand_`i'_name ==Li2 ;
		replace IsIncumbent`i' = 1 if  cand_`i'_name ==Li3 ;
		replace IsIncumbent`i' = 1 if  cand_`i'_name ==Li4 ;
		replace IsIncumbent`i' = 1 if  cand_`i'_name ==Li5 ;
		replace IsIncumbent`i' = 1 if  cand_`i'_name ==Li6 ;
		};
	};
drop Li* IsIncumbent* ;

/* calculate dependent variables */ 
#delimit;
capture gen logngaid_pc=log(ngaid_pc);
capture gen logstxls_pc=log(stxls_pc);


sort code year;
tsset code year;

capture gen F1logngaid_pc=F1.logngaid_pc;
capture gen F2logngaid_pc=F2.logngaid_pc;
capture gen F1ngaid_pc=F1.ngaid_pc;
capture gen F1logstxls_pc=F1.logstxls_pc;
capture gen F2logstxls_pc=F2.logstxls_pc;
capture gen F1stxls_pc=F1.stxls_pc;
capture gen F2logngaid_pc= F.F1logngaid_pc;
capture gen Llogngaid_pc= L.logngaid_pc;
gen income_pc= mun_taxable_income/mun_population;
gen logincome_pc=log(income_pc);
gen ey=(year==2012|year==2009|year==2005|year==2003|year==2000|
year==1996|year==1993|year==1989|year==1986|year==1983|year==1980);

/* make a unique variable for HOR electoral district-year */
 gen double year_district=10000*hor+year;

save workingJapanData_May_2018,replace;
saveold "workingJapanData_May_2018.dta", version(12) replace;

*/
#delimit; 
use "workingJapanData_May_2018.dta",clear;
/**************************************************************************/

#delimit; 

tsset code year;
/* error in population conding */ 
drop if bestLDP_VshareVP>1 & bestLDP_VshareVP~=.;
drop if sumLDP_VshareVP>1 & sumLDP_VshareVP~=.;
gen redistrict=(year>=2001);/* municipalities had a big rearragnemnt after this*/ 

	
/* generate relative rank */

#delimit;
foreach i in  bestLDP_VshareVP sumLDP_VshareVP
			logngaid_pc F1logngaid_pc lnpop bestLDPp_VshareVP 
			bestSeniorLDP_VshareVP  bestnonLDP_VshareVP  Llogngaid_pc bestnonLDPplus_VshareVP  sumnonLDPplus_VshareVP
bestloseLDP_VshareVP sumLDPp_VshareVP sumSeniorLDP_VshareVP sumnonLDP_VshareVP  
sumloseLDP_VshareVP VshareVP1 VshareVP2 VshareVP3 VshareVP4 VshareVP5 
VshareVP6 {;
	display "`i'";
	capture drop temp; 
	gsort year_d `i' -lnpop;
	egen rank = rank(`i') if `i'~=., by(year_d) ;
	by year_d: egen rr=max(rank);
	gen r`i'=(rank-1)/(rr-1); /* relative rank in terms of Best Proportionate Suppoort for winning LDP candidates*/ 
	replace r`i' =. if `i'==. | `i'==0;   
	/**************Caution: If there is No LDP winner then muni is ranked 1,. Hence drop **************/
	drop rr rank;
	};


gen best_Rank_LDPwin=max(LDP_1*win_1*rVshareVP1,LDP_2*win_2*rVshareVP2,
LDP_3*win_3*rVshareVP3,
LDP_4*win_4*rVshareVP4,LDP_5*win_5*rVshareVP5,LDP_6*win_6*rVshareVP6);	
drop rVshareVP1 rVshareVP2 rVshareVP3 rVshareVP4 rVshareVP5 rVshareVP6;
#delimit;
foreach i in sumLDP_VshareVP  bestLDP_VshareVP{;
	display "`i'";
	capture drop temp; 
	capture drop rank rr;
	sort year_d `i' lnpop;
	egen rank = rank(`i') if `i'~=., by(year_d) ;
	by year_d: egen rr=max(rank);
	gen r`i'1 = (rank==rr);
	gen r`i'2 = (rank==rr-1);
	gen r`i'3 = (rank==rr-2);
	gen r`i'Last = (rank==1);
	drop rr rank;
	};	
	
	
/************************************************************************/
/************************************************************************/
/************************************************************************/
/*  Create measure of how municipalities are broken down into Districts */ 
/************************************************************************/
/************************************************************************/
/************************************************************************/
#delimit;


gen VotesLDP =0 if  cand_01_votes~=.;
gen VotesWinLDP =0 if  cand_01_votes~=.;
#delimit; 
capture drop temp;
capture dtop temp2;
foreach t in 1 2 3 4 5 6 7 8 9 {;
	gen temp= LDP_`t'*cand_0`t'_votes;
	gen temp2= LDP_`t'*cand_0`t'_votes*win_`t';
	replace VotesLDP=VotesLDP+temp if temp~=.;
	replace VotesWinLDP=VotesWinLDP+temp2 if temp2~=.;
	drop temp temp2;
	};	
	#delimit;
foreach t in  10 11 12 13 14 15 16 17 {;
	gen temp= LDP_`t'*cand_`t'_votes;
	gen temp2= LDP_`t'*cand_`t'_votes*win_`t';
	replace VotesLDP=VotesLDP+temp if temp~=.;
	replace VotesWinLDP=VotesWinLDP+temp2 if temp2~=.;
	drop temp temp2;
	};	
label var VotesLDP "District level votes for LDP candidates";	
label var VotesWinLDP "District level votes for Winning LDP candidates";
		
#delimit;
sort year_d  code ;
by year_d: egen VotePop=sum(mun_voting_pop);
gen logVP=log(mun_voting_pop) if mun_voting_pop~=. ;
by year_d: egen sdVotePop=sd(logVP);
drop logVP;
gen fracNonV =mun_population/pop_in_electoral_district; 
gen frac= mun_voting_pop/VotePop;
#delimit;
gen data=1 if frac~=. ;
by year_d: egen POPIndex=sum(fracNonV^2);
by year_d: egen DisPop=sum(mun_population);
by year_d: egen maxfrac=max(frac);
by year_d: egen PopIndex=sum(frac^2);
by year_d: egen num =sum(data);
drop data;
by year_d: egen DisMoney=sum(ngaid_pc* mun_population); 
by year_d: egen FDisMoney=sum(exp(F1logngaid_pc)* mun_population);
by year_d: egen DisLDPvotes=sum(VotesLDP); 
by year_d: egen DisWinLDPvotes=sum(VotesWinLDP); 	
by year_d: egen DISmun_ceif=sum(mun_ceif*mun_population/DisPop);
by year_d: egen DISmun_needy_pc=sum(needy_pc*mun_population/DisPop);
by year_d: egen DISprimary_pc=sum(primary_pc*mun_population/DisPop);
by year_d: egen DISincome_pc=sum(income_pc*mun_population/DisPop);
by year_d: egen DIS_urban=sum(mun_is_seirei*mun_population/DisPop);
by year_d: egen DIS_area=sum(mun_area_size);

gen DisLogPop=log(DisPop);
gen DisLogIncome=log(DISincome_pc);
gen HI = (PopIndex-1/num)/(1-1/num);
gen logNum=log(num);


by year_d:gen first=1 if _n==1;
by year_d:gen last=1 if _n==_N;
#delimit;
gen DisLDPVS=DisLDPvotes/voting_pop_in_electoral_district;
gen DisWinLDPVS=DisWinLDPvotes/voting_pop_in_electoral_district;
gen DisMoneypervoter=DisMoney/voting_pop_in_electoral_district; 
gen FDisMoneypervoter=FDisMoney/voting_pop_in_electoral_district; 

gen FmoneyperWinLDPvote=FDisMoney/VotesWinLDP;
gen FmoneyperLDPvote=FDisMoney/VotesLDP;
gen FDismoneypc=FDisMoney/DisPop;
gen DisMoneypc=DisMoney/DisPop;

/* rescale population density AS 1000 people */ 
replace  mun_population_density=mun_population_density/1000;
gen malaportion= DisPop /(100000*totseat_in_electoral_district);
gen DisPopDensity=DisPop/(1000*DIS_area);
label var malaportion "Number of people (in 100,000s) per seat in electoral district";
label var DisPopDensity "Population Density of electoral district";

/* find rank order of DisWinLDPVS and DisLDPVS across disricts*/

#delimit;
sort year hor_electoral_district;
foreach i in DisWinLDPVS DisLDPVS {;
	capture drop temp;
	gsort year `i' -lnpop;
	egen rank = rank(`i') if `i'~=., by(year);
	by year: egen rr=max(rank);
	gen r`i'=(rank-1)/(rr-1); /* relative rank in terms of Best Proportionate Suppoort for winning LDP candidates*/ 
	drop rr rank;
	};

	
	/* Reviewer asks for the inclusion of a variable that is the municipal level voter share for the highest perfroming LDP candidate in the district: 
Lets call this variable HighLDPVS --- I will make this variable missing if no LDP winner in district.*/
#delimit;
gen HighLDPVS=0; 
foreach i in 6 5 4 3 2 1  {;
	replace HighLDPVS=VshareVP`i' if (LDP_`i'==1 & win_`i'==1 );
	};

	
/* Number of LDP candidates and number of LDPwinner */
#delimit;
gen numLDPcand=0;
gen numLDPwin=0;
#delimit;
foreach i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  {;
	replace numLDPwin=numLDPwin+1 if (LDP_`i'==1 & win_`i'==1 );
	replace numLDPcand=numLDPcand+1 if (LDP_`i'==1 );
	};



	
/* look at variance within and across municipalities */
#delimit;
gen Transfers=exp( F1logngaid_pc );
#delimit;
sort code year; 
tsset code year; 
capture gen lagT=L.Transfers;
xtsum Tran rbestLDP_VshareVP rsumLDP_VshareVP 
rF1logngaid_pc  ;
/* pre-reform*/
  xtsum Tran  rbestLDP_VshareVP rsumLDP_VshareVP 
rF1logngaid_pc if year<1994;

 
* drop senior_*  LDP_*plus COM_* Vshare* VshareVP* PropSupport*;

#delimit;
saveold tempMay2018,replace version(12);

